package com.mkait.thief.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;

public class WrapResponseMapper {
    private WrapResponseMapper() {
    }

    public static <E> ResponseEntity<ResponseWrapper<E>> wrap(@Nullable String code, @Nullable String message, @Nullable E data) {
        return wrap(code, message, (String)null, (String)null, data, HttpStatus.OK);
    }

    public static <E> ResponseEntity<ResponseWrapper<E>> wrap(@Nullable String code, @Nullable String message, String subCode, String subMsg, @Nullable E data) {
        return wrap(code, message, subCode, subMsg, data, HttpStatus.OK);
    }

    public static <E> ResponseEntity<ResponseWrapper<E>> wrap(@Nullable String code, @Nullable String message, String subCode, String subMsg, @Nullable E data, HttpStatus httpStatus) {
        if (StringUtils.isBlank(subCode)) {
            subCode = "";
        }

        if (StringUtils.isBlank(subMsg)) {
            subMsg = "";
        }

        return new ResponseEntity(new ResponseWrapper(code, message, subCode, subMsg, data), httpStatus);
    }

    public static <E> ResponseEntity<ResponseWrapper<E>> wrap(@Nullable String code, @Nullable String message) {
        return wrap(code, message, (E)null);
    }

    public static <E> ResponseEntity<ResponseWrapper<E>> wrap(@Nullable String code, @Nullable String message, @Nullable String subCode, @Nullable String subMsg) {
        return wrap(code, message, subCode, subMsg, (E)null);
    }

    public static <E> ResponseEntity<ResponseWrapper<E>> wrap(String code) {
        return wrap(code, (String)null);
    }

    public static <E> ResponseEntity<ResponseWrapper<E>> wrap(Exception exception) {
        return wrap("500", exception.getMessage());
    }

    public static <E> E unWrap(ResponseWrapper<E> ResponseWrapper) {
        return ResponseWrapper.getData();
    }

    public static <E> ResponseEntity<ResponseWrapper<E>> illegalArgument() {
        return wrap("403", "参数非法");
    }

    public static <E> ResponseEntity<ResponseWrapper<E>> unauthorized(String subCode, String subMsg) {
        return wrap("401", ResponseWrapper.UNAUTHORIZED_MESSAGE, subCode, subMsg, (E)null, HttpStatus.UNAUTHORIZED);
    }

    public static <E> ResponseEntity<ResponseWrapper<E>> error() {
        return wrap("500", "内部异常");
    }

    public static <E> ResponseEntity<ResponseWrapper<E>> error(String message) {
        return wrap("500", StringUtils.isBlank(message) ? "内部异常" : message);
    }

    public static <E> ResponseEntity<ResponseWrapper<E>> error(String message, @Nullable String subCode, @Nullable String subMsg) {
        return wrap("500", StringUtils.isBlank(message) ? "内部异常" : message, subCode, subMsg);
    }

    public static ResponseEntity<ResponseWrapper> ok() {
        return ResponseEntity.ok(new ResponseWrapper());
    }

    public static <E> ResponseEntity<ResponseWrapper<E>> ok(E o) {
        return ResponseEntity.ok(new ResponseWrapper("200", "操作成功", o));
    }

}